//
//  PingAssistor.m
//  iOSLaboratory
//
//  Created by yangshansi on 2017/4/19.
//  Copyright © 2017年 WiLL. All rights reserved.
//

#import "PingTask.h"

#import "OnePingAction.h"




@interface PingTask () 

@end



@implementation PingTask

/******************************************************************************/
/**** Static Method                                                        ****/
/******************************************************************************/
#pragma mark - Static Method

+ (instancetype)pingTaskWithHost:(NSString *)hostName
                      retryCount:(NSInteger)retryCount
                         timeout:(NSInteger)timeout
                            data:(NSData *)data
                      pingMethod:(PingMethod)pingMethod
{
    return [[PingTask alloc] initWithHost:hostName retryCount:retryCount
                                  timeout:timeout data:nil pingMethod:pingMethod];
}

+ (instancetype)pingTaskWithHost:(NSString *)hostName
                      retryCount:(NSInteger)retryCount
                            data:(NSData *)data
                      pingMethod:(PingMethod)pingMethod
{
    return [PingTask pingTaskWithHost:hostName retryCount:retryCount
                              timeout:5 data:data pingMethod:pingMethod];
}

+ (instancetype)pingTaskWithHost:(NSString *)hostName
                            data:(NSData *)data
                      pingMethod:(PingMethod)pingMethod

{
    return [PingTask pingTaskWithHost:hostName retryCount:1
                              timeout:5 data:data pingMethod:pingMethod ];
}




/******************************************************************************/
/**** Customized Method                                                    ****/
/******************************************************************************/
#pragma mark - Customized Method

- (PingTaskResult *)doTask
{
    self.result.hostName  = self.hostName;
    self.result.retryCount = self.retryCount;
    self.result.sumTime = 0;

    for (int retryTimes = 0; retryTimes<self.retryCount; retryTimes++)
    {
       [self doOnePingAction:self.pingMethod
               sequenceNumber:retryTimes
                         host:self.hostName
                      timeout:self.timeout
                         data:self.data
                       result:self.result];
    }

    self.result.aveTime = (self.result.sumTime / self.result.sendCount);

    return self.result;
}


- (void)doOnePingAction:(PingMethod)pingMethod
         sequenceNumber:(NSInteger)sequenceNumber
                   host:(NSString *)hostName
                timeout:(NSInteger)timeout
                   data:(NSData *)data
                 result:(PingTaskResult *)result
{
    OnePingAction *onePing = [OnePingAction onePingActionWithMethod:pingMethod
                                                     sequenceNumber:sequenceNumber
                                                               host:hostName
                                                            timeout:timeout
                                                               data:data];
    [onePing doOnePing];
    
    if (onePing.ip != nil && [onePing.ip isEqualToString:@""] == NO) {
        result.ip = onePing.ip;
    }
    
    if (onePing.sendTime > 0) {
        result.sendCount += 1;
    }
    if (onePing.recvTime > 0) {
        result.recvCount += 1;
    }
    if (onePing.interval > 0) {
        result.sumTime += onePing.interval;
    }

}





/******************************************************************************/
/**** Lifecycle Method                                                     ****/
/******************************************************************************/
#pragma mark - Lifecycle Method

- (instancetype)initWithHost:(NSString *)hostName
                  retryCount:(NSInteger)retryCount
                     timeout:(NSInteger)timeout
                        data:(NSData *)data
                  pingMethod:(PingMethod)pingMethod
{
    self = [super init];
    if (self) {
        self->_hostName   = hostName;
        self->_retryCount = retryCount;
        self->_timeout    = timeout;
        self->_data       = data;
        self->_pingMethod = pingMethod;
        self->_pingList   = [NSMutableArray new];
        
        self->_result = [PingTaskResult new];
        
    }
    return self;
}













@end
